package com.hrm;

import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.hrm.util.Config;
import com.hrm.util.GeneratorUtils;
import com.hrm.util.MyColumn;
import com.hrm.util.MyTable;

public class Generator {
	static List<String> ignoreTableName = new ArrayList<String>();
	static{
	}
	
	public static void connection(){
		
	}
	
	public static void createFile(String tableName,Connection connection,ResultSet rs) throws SQLException {
		 	MyTable myTable = new MyTable();
	        myTable.setTableName(tableName);
	        if(ignoreTableName.contains(tableName.toUpperCase())) return;
	        myTable.setEntityFileName(GeneratorUtils.TableName2FileName(tableName));
//	        DatabaseMetaData dbmd=connection.getMetaData();
//	        ResultSet rsIndex = dbmd.getIndexInfo(null, null, tableName, false, false);   
//            while (rsIndex.next()) {   
//            	if(!"id".equals(rsIndex.getString(9))){
//            		myTable.setUniqueColName(rsIndex.getString(9));
//            	}
//            	System.out.println(rsIndex.getString(1));
//            	System.out.println(rsIndex.getString(2));
//            	System.out.println(rsIndex.getString(3));
//            	System.out.println(rsIndex.getString(4));
//            	System.out.println(rsIndex.getString(5));
//            	System.out.println(rsIndex.getString(6));
//            	System.out.println(rsIndex.getString(7));
//            	System.out.println(rsIndex.getString(8));
//            	System.out.println(rsIndex.getString(9));
//            	System.out.println(rsIndex.getString(10));
//            	System.out.println(rsIndex.getString(11));
//            	System.out.println(rsIndex.getString(12));
//            	System.out.println(rsIndex.getString(13));
//            }   
	        List<MyColumn> list=new ArrayList<MyColumn>();
	        String sql = "select * from "+tableName;

	        Statement state = connection.createStatement();
	        rs = state.executeQuery(sql);
	        ResultSetMetaData rsmd = rs.getMetaData() ;
	       
	        for(int i = 1; i <= rsmd.getColumnCount(); i++)
	        {
	        	String colname = rsmd.getColumnName(i);
	        	String coltype = rsmd.getColumnTypeName(i);
	        	int colsize = rsmd.getColumnDisplaySize(i);
	        	int n = rsmd.isNullable(i);
	        	boolean nullable = true;
	        	 switch (n) {
              case 0: // '/0'
                  nullable = false;
                  break;

              case 1: // '/001'
                  nullable = true;
                  break;

              default:
                  nullable = true;
                  break;
              }
	        	MyColumn myColumn = new MyColumn(colname, coltype, colsize, nullable);
	        	list.add(myColumn);
	        }
	        myTable.setList(list);
	        System.out.println("-----------------"+myTable.getEntityFileName()+"----------------------");
	        if(GeneratorUtils.createEntityFile(Config.ENTITYPACKAGE, myTable))
	        	System.out.println("生成实体类"+myTable.getEntityFileName()+".java成功!");
	        if(GeneratorUtils.createDAOFile(Config.DAOPACKAGE, myTable))
	        	System.out.println("生成DAO接口文件"+myTable.getEntityFileName()+"Mapper.java成功!");
	        if(GeneratorUtils.createMapperFile(Config.MAPPERPACKAGE, myTable))
	        	System.out.println("生成映射文件"+myTable.getEntityFileName()+"Mapper.xml成功!\n");
//	        rsIndex.close();
	}
	public static void main(String[] args) {
		try {
			Class.forName(Config.DRIVERNAME);
			Connection connection = DriverManager.getConnection(Config.URL,
					Config.USERNAME, Config.PASSWORD);
			DatabaseMetaData dbmd = connection.getMetaData();
	        ResultSet rs = null;
	        String[] typeList = new String[] { "TABLE" };
	        rs = dbmd.getTables("", "", null, typeList);
	        List<String> entities=new ArrayList<String>();
	        while (rs.next()) {
	            String tableName = rs.getString("TABLE_NAME");
	            String type = rs.getString("TABLE_TYPE");
	            if (type.equalsIgnoreCase("table") && tableName.indexOf("$") == -1){
	                createFile(tableName, connection, rs);
	            }
	            entities.add(GeneratorUtils.TableName2FileName(tableName));
	        }
	        if(Config.CREATEMYBATISCONFIGXML){
	        	if(GeneratorUtils.createSqlMapConfigXML(entities))
	        		System.out.println("生成Mybatis配置文件sqlMapConfig.xml成功!");
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
